﻿using System;
using System.Collections.Generic;
using System.Text;

namespace CL2036.Protocol
{
    internal static class CL2036Protocol
    {
        /// <summary>
        /// 联机组帧
        /// </summary>
        /// <returns></returns>
        public static byte[] WeaveLink()
        {
            ByteBuffer buf = new ByteBuffer();
            buf.Put(0xA3);
            buf.PutUShort(4);
            buf.Put(1);
            buf.Put(1);
            return PublicFunction.CLT.GetFrameData(0x10, 0x80, buf.ToByteArray());
        }
        /// <summary>
        /// 联机拆帧
        /// </summary>
        /// <param name="vData"></param>
        /// <returns></returns>
        public static bool ExplainLink(byte[] vData)
        {
            return isReplayOk(vData);
        }

        /// <summary>
        /// 解析升源结果
        /// </summary>
        /// <param name="vData"></param>
        /// <returns></returns>
        public static bool ExplainPowerOn(byte[] vData)
        {
            return isReplayOk(vData);
        }
        /// <summary>
        /// 升源组包
        /// </summary>
        /// <param name="clfs"></param>
        /// <param name="U"></param>
        /// <returns></returns>
        public static List<byte[]> WeavePowerOn(int clfs, float U)
        {
            if (U == 100f || U == 380F)
            {
                clfs = 2;
            }
            List<byte[]> lstData = new List<byte[]>();
            lstData.Add(setTestPoint(U, (byte)clfs));
            //lstData.Add(updatePhi());        //由于V80软件升源未设置角度，因此为了统一将此处去掉了
            lstData.Add(updatePowerOn());
            return lstData;
        }

        /// <summary>
        /// 关源组帧
        /// </summary>
        /// <returns></returns>
        public static byte[] WeavePowerOff()
        {
            ByteBuffer buf = new ByteBuffer();
            buf.Put(0xa3);
            buf.Put(0x05);       //page5
            buf.Put(0x44);       //对group2, group6操作
            buf.Put(0x80);       //对data07的数据操作(group2)
            buf.Put(0x07);       //全部更新
            buf.Put(0x03);       //对data00，data01的数据操作(group6)
            buf.Put(0xbf);       //更新电压电流状态

            return PublicFunction.CLT.GetFrameData(0x10, 0x80, buf.ToByteArray());

        }
        /// <summary>
        /// 关源返回命令拆帧
        /// </summary>
        /// <param name="vData"></param>
        /// <returns></returns>
        public static bool ExplainPowerOff(byte[] vData)
        {
            return isReplayOk(vData);
        }
        /// <summary>
        /// 脱机组包
        /// </summary>
        /// <returns></returns>
        public static byte[] WeaveOffLine()
        {
            ByteBuffer buf = new ByteBuffer();
            buf.Put(0xA3);
            buf.PutUShort(4);
            buf.Put(1);
            buf.Put(0);
            return PublicFunction.CLT.GetFrameData(0x10, 0x80, buf.ToByteArray());

        }

        /// <summary>
        ///  脱机解包
        /// </summary>
        /// <param name="vData"></param>
        /// <returns></returns>
        public static bool ExplainOffLine(byte[] vData)
        {
            return isReplayOk(vData);
        }


        private static bool isReplayOk(byte[] vData)
        {
            return (vData[4] == 0x30);
        }

        private static byte[] setTestPoint(float U, byte clfs)
        {

            ByteBuffer buf = new ByteBuffer();
            buf.Put(0xA3);      //命令字 //A3
            buf.Put(0);         //page0 00
            buf.Put(0xc0);      //对group6 group7操作 C0
            buf.Put(0xff);      //对data00-data07的数据操作FF
            //压入电压
            buf.PutInt_S((int)(U * 10000));  //额定电压  C0912100
            buf.Put(0xfc);                  //指数:-4 fc
            buf.PutInt_S((int)(1 * 10000));            //UC 10270000
            buf.PutInt_S((int)(1 * 10000));          //UB   10270000
            buf.PutInt_S((int)(1 * 10000));          //UA   10270000
            buf.Put(clfs);                          //测量方式 00
            buf.PutInt_S((int)(1.0 * 10000));  //功率因数 10270000
            buf.PutInt_S(50 * 10000);       //频率 20A10700
            buf.Put((byte)0);        //元件 00
            buf.Put(0xbf);      //对data00-data05 data07的数据操作 BF
            buf.PutInt_S((int)(150 * 10000));       //In 60E31600
            buf.Put(0xfa);                  //小数位 fa
            buf.PutInt_S((int)(600 * 10000)); //808D5B00
            buf.Put(0xfa);                  //小数位 fa
            buf.PutInt_S((int)(0 * 10000));          //Ic 00000000
            buf.PutInt_S((int)(0 * 10000));          //Ib 00000000
            buf.PutInt_S((int)(0 * 10000));          //Ia 00000000
            buf.Put(0);                     //Ib为基准00
            buf.Put(1);         //极性 01 46
            return PublicFunction.CLT.GetFrameData(0x10, 0x80, buf.ToByteArray());

            //return PublicFunction.CLT.GetFrameData(0x10,0x80,
        }
        private static byte[] updatePhi()
        {
            ByteBuffer buf = new ByteBuffer();
            buf.Put(0xa3);
            buf.Put(0x05);
            buf.Put(0x02);
            buf.Put(0x3f);
            Single[] sngPhi = new Single[] { 0, 240, 120, 0, 240, 120 };
            for (byte b = 0; b < sngPhi.Length; b++)
                buf.PutInt_S((int)(sngPhi[b] * 10000));
            return PublicFunction.CLT.GetFrameData(0x10, 0x80, buf.ToByteArray());

        }
        private static byte[] updatePowerOn()
        {
            ByteBuffer buf = new ByteBuffer();
            buf.Put(0xa3);
            buf.Put(0x05);       //page5
            buf.Put(0x44);       //对group2, group6操作
            buf.Put(0x80);       //对data07的数据操作(group2)
            buf.Put(0x07);       //全部更新
            buf.Put(0x03);       //对data00，data01的数据操作(group6)

            buf.Put(0x3f);
            buf.Put(0xbf);
            return PublicFunction.CLT.GetFrameData(0x10, 0x80, buf.ToByteArray());

        }
    }
}

